package com.hitplane.manager 
{
	import com.hitplane.debug.DebugTrace;
	import com.hitplane.interfaces.ITick;
	
	import flash.events.KeyboardEvent;

	/**
	 * 按键控制 实现Itick
	 * 单例模式类
	 * @author rrDog
	 */
	public class KeyBoardManager  implements ITick
	{
		private static var _instance:KeyBoardManager = null;
		public function KeyBoardManager(single:Single) 
		{
			
			if(single == null)
			{
				throw new Error("Can't create instance , Single is Null!");
			}
			
			timeGap=20;
		}
		/**
		 * 单例引用
		 */
		public static function get instance():KeyBoardManager
		{
			if(_instance == null)
			{
				_instance = new KeyBoardManager(new Single());
			}
			return _instance;
		}
		//start===============================================================
		/**
		 * 以按键ASC码为	key
		 * 执行方法为		value(Array)	存储
		 */
		private var _keyBoardArr:Array;
		
		//按下的键集合
		private var _downKeyArr:Array;
		
		//存储key的前置标识
		public static const KEY_PRE:String = "ASCII";
		
		/**
		 * 注册 
		 * @param keyStr
		 * @param fun
		 * 
		 */
		public function registerKey(keyStr:String,fun:Function):void {
			if (_keyBoardArr == null)_keyBoardArr = [];
			_keyBoardArr[keyStr] = _keyBoardArr[keyStr] || [];
			var index:int = _keyBoardArr[keyStr].indexOf(fun);
			if (index == -1) {
				_keyBoardArr[keyStr].push(fun);
			}else {
				DebugTrace.traceObj(this, "已经注册。");
			}
		}
		/**
		 * 移除 
		 * @param keyStr
		 * @param fun
		 * 
		 */
		public function removeKey(keyStr:String,fun:Function):void {
			if (_keyBoardArr == null) return;
			if (_keyBoardArr[keyStr] == null) return;
			var index:int = _keyBoardArr[keyStr].indexOf(fun);
			_keyBoardArr[keyStr].splic(index, 1);
			if (_keyBoardArr[keyStr].length <= 0)_keyBoardArr[keyStr] = null;
		}
		/**
		 * 是否存在 
		 * @param keyStr
		 * @return 
		 * 
		 */
		public function hasKey(keyStr:String):Boolean {
			if (_keyBoardArr == null) return false;
			if (_keyBoardArr[keyStr] == null) return false;
			return true;
		}
		/**
		 * 开始对按键的判定
		 */
		public function startKey():void {
			GameContainManager.instance.mainStage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			GameContainManager.instance.mainStage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		/**
		 * 停止对按键的判定
		 */
		private var _createTime:int;
		private var _executeTime:int;
		private var _timeGap:int;
		
		public function stopKey():void {
			GameContainManager.instance.mainStage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			GameContainManager.instance.mainStage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		//实现接口Itick===========================================
		public function updateForTick(tickTime:int):void{
			if(_createTime==0)return;
			var totalTime:int = (tickTime-_createTime) / timeGap;
			if (totalTime > _executeTime) {
				var difference:int = totalTime-_executeTime;
				while (difference) {
					onTick();
					difference--;
				}
				_executeTime = totalTime;
			}
		}
		public function onTick():void{
			if(_downKeyArr==null)return;
			var i:int = _downKeyArr.length;
			if(i<=0)return;
			var tempDownKeyArr:Array=_downKeyArr.concat();
			var keyStr:String;
			while (i>0) {
				keyStr=tempDownKeyArr.shift();
				if (hasKey(keyStr)) {
					for each (var item:Function in _keyBoardArr[keyStr]) 
					{
						item();
					}
				}
				i--;
			}
		}
		public function set createTime(value:int):void{
			_createTime=value;
		}
		public function get createTime():int{
			return _createTime
		}
		//时间间隔
		public function set timeGap(value:int):void{
			_timeGap=value;
		}
		public function get timeGap():int{
			return _timeGap;
		}
		
		//执行过的次数
		public function set executeTime(value:int):void{
			_executeTime=value;
		}
		public function get executeTime():int{
			return _executeTime;
		}
		//private===============================================================
		private function isKeyRegister(keyStr:String):Boolean{
			if(_keyBoardArr==null)return false;
			if(_keyBoardArr[keyStr]==null)return false;
			return true;
	
		}
		/**
		 * 不支持复合键
		 * @param	e
		 */
		private function onKeyDown(e:KeyboardEvent):void 
		{
			var keyStr:String=KEY_PRE + e.keyCode;
			var isReg:Boolean=isKeyRegister(keyStr);
			if(!isReg)return;
			if (_downKeyArr == null)_downKeyArr = [];
			//按下的键如果没有在_downKeyArr中，push到_downKeyArr中
			var index:int=_downKeyArr.indexOf(keyStr);
			if(index==-1)_downKeyArr.push(keyStr);
		}
		private function onKeyUp(e:KeyboardEvent):void 
		{
			var keyStr:String=KEY_PRE + e.keyCode;
			var isReg:Boolean=isKeyRegister(keyStr);
			if(!isReg)return;
			var index:int = _downKeyArr.indexOf(keyStr);
			if (index != -1)_downKeyArr.splice(index, 1);
		}
	}
}
class Single{}